package core.proto.annotation;

import core.proto.Util;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import static app.constants.CollabVizConstants.*;

public class AnnotationModel {
	 
	public enum AnnotationLocation {
		TIMELINE,
		MAP,
		DOC
	}
	
	protected String userText; // user entered annotation
	protected boolean delete; // whether we are deleting this annotation when saving. 

	protected String docKey; // what document this annotation is tied to. 

	protected AnnotationLocation location; // what 
		
	protected AnnotationModel(String userText, String docKey, AnnotationLocation location) {
		this.userText = userText;
		this.delete = false;
		this.docKey = docKey;
		this.location = location;
	}
	
	protected AnnotationModel (String[] tokens) {
		this.location = AnnotationLocation.valueOf(tokens[0]);
		this.docKey = tokens[1];
		this.userText = this.restoreNewlines(tokens[2]);
	}
	
	public boolean isDelete() {
		return delete;
	}

	public void setDelete(boolean delete) {
		this.delete = delete;
	}

	public String getDocKey() {
		return docKey;
	}
	public String getUserText() {
		return userText;
	}
	public void setUserText(String userText) {
		this.userText = userText;
	}
	public AnnotationLocation getLocation() {
		return location;
	}
	
	public String toSaveString () {
		return toBaseSaveString();
	}
	
	protected String toBaseSaveString () {
		return Util.joinString(ANNOTATION_DELIMITER, location, docKey, stripMetaCharacters(userText));
	}

	protected String restoreNewlines(String input) {
		return input.replace("\\n", "\n");
	}

	protected String stripMetaCharacters(String input) {
		// prevent users from messing up input formatting when we load up the data again 
		// since we will interpret "\\n" as the newline character. normal users should 
		// never enter "\\n" anyway, so it should be a safe transformation. 
		while(input.contains("\\n")){
			input = input.replace("\\n", "");
		}
		input = input.replace(ANNOTATION_DELIMITER, ""); 
		return input.replace("\n", "\\n");
	}


}
